import { defineStore } from 'pinia'
import { ref, computed } from 'vue'
import { userAPI } from '@/apis/user.js'
import { ElMessage } from 'element-plus'

export const useUserStore = defineStore('user', () => {
  // 状态
  const token = ref('')
  const userInfo = ref({})
  const isLoggedIn = computed(() => !!token.value)
  const unreadCount = ref({})

  // 登录
  const login = async (loginData) => {
    try {
      const response = await userAPI.login(loginData)
      if (response.status === 'success') {
        token.value = response.data.token
        userInfo.value = response.data
        ElMessage.success("Login successful!")
        return response
      }
    } catch (error) {
      console.log(error)
    }
  }

  // 注册
  const register = async (registerData) => {
    try {
      const response = await userAPI.register(registerData)
      if (response.status === 'success') {
        ElMessage.success('Registration successful!')
        return response
      }
    } catch (error) {
      console.log(error)
    }
  }

  // 退出登录
  const logout = async () => {
    try {
      await userAPI.logout()
      ElMessage.success('Logout successful!')
    } catch (error) {
      console.log(error)
    } finally {
      removeInfo()
    }
  }


  // 更新用户信息
  const updateUserInfo = async (updateData) => {
    try {
      const response = await userAPI.updateUserInfo(updateData)
      if (response.status === 'success') {
        userInfo.value = { ...userInfo.value, ...response.data }
        ElMessage.success('Successfully updated user info')
        return response
      }
    } catch (error) {
      console.log(error)
    }
  }

  // 上传头像
  const uploadAvatar = async (formData) => {
    try {
      const response = await userAPI.uploadAvatar(formData)
      if (response.status === 'success') {
        userInfo.value.avatar = response.data.avatar
        ElMessage.success('头像上传成功')
        return response
      }
    } catch (error) {
      console.log(error)
    }
  }

  // 获取消息数量
  const fetchMessageCount = async () => {
    try {
      const response = await userAPI.getMessageCount()
      if (response.status === 'success') {
        return response.data
      }
    } catch (error) {
      console.log(error)
    }
  }

  // 获取消息列表
  const fetchMessageList = async (params) => {
    try {
      const response = await userAPI.getMessageList(params)
      if (response.status === 'success') {
        return response.data
      }
    } catch (error) {
      console.log(error)
    }
  }

  const fetchUnreadMessageCount = async () => {
    try {
      const response = await userAPI.getUnreadMessageCount()
      if (response.status === 'success') {
        unreadCount.value = response.data
        return response.data
      }
    } catch (error) {
      console.log(error)
    }
  }

  // 清除用户信息
  const removeInfo = () => {
    token.value = ''
    userInfo.value = {}
    unreadCount.value = {}
  }

  return {
    // 状态
    token,
    userInfo,
    isLoggedIn,
    unreadCount,
    
    // 方法
    login,
    register,
    logout,
    updateUserInfo,
    uploadAvatar,
    fetchMessageCount,
    fetchMessageList,
    removeInfo,
    fetchUnreadMessageCount
  }
}, {
  persist: true
})
